﻿using System;
using System.Collections.Generic;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class CustomerGroupBiz
    {
        #region Properties

        private readonly ICustomerGroupDao _customerGroupDao;

        #endregion

        #region Constructors

        public CustomerGroupBiz()
        {
            _customerGroupDao = new CustomerGroupDao();
        }

        public CustomerGroupBiz(ICustomerGroupDao customerGroupDao)
        {
            _customerGroupDao = customerGroupDao;
        }

        #endregion

        #region CustomerGroup Methods

        public CustomerGroup GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _customerGroupDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public CustomerGroup CreateGroup(CustomerGroup customerGroup)
        {
            // validate inputs
            if (customerGroup == null) return null;
            if (string.IsNullOrEmpty(customerGroup.Code) || string.IsNullOrEmpty(customerGroup.Name)) return null;

            try
            {
                var addedCustomerGroup = _customerGroupDao.AddCustomerGroup(customerGroup);
                return addedCustomerGroup;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public void UpdateGroup(CustomerGroup customerGroup)
        {
            if (customerGroup == null) return;
            try
            {
                _customerGroupDao.UpdateCustomerGroup(customerGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GroupListWs()
        {
            try
            {
                var peopleList = _customerGroupDao.GetAllCustomerGroups();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateGroupWs(CustomerGroup customerGroup)
        {
            // validate inputs
            if (customerGroup == null) return null;
            if (string.IsNullOrEmpty(customerGroup.Code) || string.IsNullOrEmpty(customerGroup.Name)) return null;

            try
            {
                var addedCustomerGroup = _customerGroupDao.AddCustomerGroup(customerGroup);
                return new { Result = "OK", Record = addedCustomerGroup };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateGroupWs(CustomerGroup customerGroup)
        {
            if (customerGroup == null) return null;
            try
            {
                _customerGroupDao.UpdateCustomerGroup(customerGroup);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteGroupWs(int customerGroupId)
        {
            try
            {
                _customerGroupDao.DeleteCustomerGroup(customerGroupId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public int GetMaxId()
        {
            return _customerGroupDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _customerGroupDao.GetCurrentIdentity() + 1;
        }

        #endregion
        
        #region Helpers

        #endregion
    }
}
